package com.telenav.serverproxy;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.telenav.comm.Comm;
import com.telenav.comm.Host;
import com.telenav.comm.ICommCallback;

public abstract class AbstractJsonServerProxy extends AbstractServerProxy {

	public AbstractJsonServerProxy(Host host, Comm comm,
			IServerProxyListener serverProxyListener) {
		super(host, comm, serverProxyListener);
	}

	@Override
	protected void handleResponse(byte[] binData) {
		// TODO Auto-generated method stub
		try
        {
            if (binData == null || binData.length == 0)
            {
                this.status = ICommCallback.NO_DATA;
            }
            else
            {
                String response = new String(binData);
                JSONTokener jsonTokener = new JSONTokener(response);
                JSONObject jsonObject = (JSONObject) jsonTokener.nextValue();
                if(jsonObject != null)
                {
                    parseRequestSpecificData(jsonObject);
                }
            }
        }
        catch (Exception t)
        {
            this.status = ICommCallback.RESPONSE_ERROR;
        }
	}
	
    protected void sendRequest(byte[] request, String action) throws IOException
    {
        
    	//this need added the client common parameters
    	sendData(request, action);
    }

	protected abstract void parseRequestSpecificData(String action, JSONObject jsonObject) throws JSONException;
	
	protected abstract void parseRequestSpecificData(JSONObject jsonObject) throws JSONException;
}
